package com.niodata.dp.app.dao;

import com.niodata.dp.app.AppVariable;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface VariableMapper {

  @Delete("delete from APP_VARIABLE WHERE APP_ID=#{appId}")
  public void deleteAppVariables(@Param("appId") String appId);

  @Delete("delete from APP_VARIABLE WHERE APP_ID=#{appId} and NAME=#{name}")
  public void delete(@Param("appId") String appId, @Param("name") String name);

  @Insert("insert into APP_VARIABLE(NAME,APP_ID,APP_CODE,TYPE,BIND,BIND_TASK,BIND_JOB,BIND_PARAM,"
      + "INIT_TIME,BIND_TIME,CURRENT_VALUE,PRE_VALUE,DESCRIPTION,UP_TIME,PARAM_TYPE) "
      + "VALUES"
      + "(#{var.name},#{var.appId},#{var.appCode},#{var.type},"
      + "#{var.bind},#{var.bindTask},#{var.bindJob},"
      + "#{var.bindParam},#{var.initTime},#{var.bindTime},"
      + "#{var.currentValue,"
      + "typeHandler=com.niodata.dp.app.dao.handler.VariableValueTypeHandler},"
      + "#{var.previousValue,"
      + "typeHandler=com.niodata.dp.app.dao.handler.VariableValueTypeHandler},"
      + "#{var.description},#{var.upTime},#{var.paramType})")
  public void save(@Param("var") AppVariable var);

  @Select("SELECT * FROM APP_VARIABLE where APP_ID=#{appId} and NAME =#{name}")
  @ResultMap("variableResultMap")
  public AppVariable getByName(@Param("appId") String appId, @Param("name") String name);

  @Select("select * from APP_VARIABLE where APP_CODE=#{appCode} and NAME=#{name}")
  @ResultMap("variableResultMap")
  public AppVariable getByAppCode(@Param("appCode") String appCode, @Param("name") String name);

  public List<AppVariable> getAppVariables(@Param("userId") long userId,
      @Param("appId") String appId,
      @Param("keyWord") String keyWord,
      @Param("offset") int offset,
      @Param("count") int count);

  @Update("update APP_VARIABLE set BIND=#{var.bind},BIND_TASK=#{var.bindTask},"
      + "BIND_JOB=#{var.bindJob},"
      + "BIND_PARAM=#{var.bindParam},INIT_TIME=#{var.initTime},"
      + "BIND_TIME=#{var.bindTime},"
      + "CURRENT_VALUE=#{var.currentValue,"
      + "typeHandler=com.niodata.dp.app.dao.handler.VariableValueTypeHandler},"
      + "PRE_VALUE=#{var.previousValue,"
      + "typeHandler=com.niodata.dp.app.dao.handler.VariableValueTypeHandler},"
      + "DESCRIPTION=#{var.description},"
      + "UP_TIME=#{var.upTime},"
      + "PARAM_TYPE=#{var.paramType} where APP_ID=#{var.appId} and NAME=#{var.name}")
  public void update(@Param("var") AppVariable var);

  @Select("select * from APP_VARIABLE WHERE BIND_TASK=#{taskName} and BIND_JOB=#{jobName}")
  @ResultMap("variableResultMap")
  public List<AppVariable> getAppVariablesByJob(@Param("taskName") String taskName,
      @Param("jobName") String jobName);

  @Select("select case when exists (select 1 from APP_VARIABLE"
      + " where APP_ID=#{appId}) then 1 else 0 end")
  public boolean existsVariablesInApp(@Param("appId") String appId);
}
